﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using com.haoest.Forms.Mouse;
using System.Runtime.InteropServices;

namespace com.haoest.Forms
{

    /// <summary>
    /// UserControl型的FormContainer类
    /// <para>不推荐使用，这个已经淘汰</para>
    /// </summary>
    public partial class FormContainer : UserControl
    {
        public FormContainer()
        {
            InitializeComponent();
            this.Dock = DockStyle.Fill;
        }


        #region 声明变量和常量
        const int AW_HOR_POSITIVE = 0x0001;         //从左向右显示
        const int AW_HOR_NEGATIVE = 0x0002;         //从右到左显示
        const int AW_VER_POSITIVE = 0X0004;         //从上到下显示
        const int AW_VER_NEGATIVE = 0x0008;         //从下到上显示         
        const int AW_CENTER = 0x0010;               //若使用了AW_HIDE标志，则使窗口向内重叠，即收缩窗口；否则使窗口向外扩展，即展开窗口
        const int AW_HIDE = 0x10000;                //隐藏窗口，缺省则显示窗口
        const int AW_ACTIVATE = 0x20000;            //激活窗口。在使用了AW_HIDE标志后不能使用这个标志
        const int AW_SLIDE = 0x40000;               //使用滑动类型。缺省则为滚动动画类型。当使用AW_CENTER标志时，这个标志就被忽略
        const int AW_BLEND = 0x80000;               //透明度从高到低
        private String formName = null;             //窗口名
        private Image formIconImage = null;         //窗口图标
        private int resizeBorderWidth = 2;          //拉伸时边框宽度
        private Color menuBackColor = Color.Empty;  //菜单背景色
        private Color titleBackColor = Color.Empty; //标题栏背景色
        private Color topBackColor = Color.Empty;   //顶部背景色
        private MenuStrip mainMenu = null;          //主菜单
        private MouseAction mouse = null;           //鼠标，用来改变鼠标形状
        #endregion

        #region 属性设置
        /// <summary>
        /// 菜单栏背景
        /// </summary>
        [Browsable(true)]
        [Description("菜单栏背景颜色")]
        public Color MenuBackColor
        {
            get
            {
                if (menuBackColor == Color.Empty) menuBackColor = Color.Transparent;
                return menuBackColor;
            }
            set
            {
                menuBackColor = value;
                MainMenu.BackColor = menuBackColor;
            }
        }

        /// <summary>
        /// 标题栏背景
        /// </summary>
        [Browsable(true)]
        [Description("标题栏背景颜色")]
        public Color TitleBackColor
        {
            get
            {
                if (titleBackColor == Color.Empty) titleBackColor = Color.Transparent;
                return titleBackColor;
            }
            set
            {
                titleBackColor = value;
                LabelWindowName.BackColor = titleBackColor;
            }
        }
        /// <summary>
        /// 顶部背景
        /// </summary>
        [Browsable(true)]
        [Description("顶部背景颜色")]
        public Color TopBackColor
        {
            get
            {
                if (topBackColor == Color.Empty) topBackColor = Color.Transparent;
                return topBackColor;
            }
            set
            {
                topBackColor = value;
                TableLayoutPanelTop.BackColor = topBackColor;
            }
        }

        /// <summary>
        /// 主菜单
        /// </summary>
        [Browsable(true)]
        [Description("窗口主菜单")]
        public MenuStrip MainMenu
        {
            get
            {
                if (mainMenu == null)
                {
                    TableLayoutPanelTop.Controls.Remove(mainMenu);
                    mainMenu = DefaultMenuStrip;
                    mainMenu.Visible = true;
                    TableLayoutPanelTop.Controls.Add(mainMenu, 1, 1);
                }
                mainMenu.Refresh();
                return mainMenu;
            }
            set
            {
                TableLayoutPanelTop.Controls.Remove(mainMenu);
                mainMenu = value;
                mainMenu.BackColor = MenuBackColor;
                TableLayoutPanelTop.Controls.Add(mainMenu, 1, 1);
                mainMenu.Refresh();
            }
        }

        /// <summary>
        /// 窗口图标
        /// </summary>
        [Browsable(true)]
        [Description("窗口图标")]
        public Image FormIconImage
        {
            get
            {
                if (formIconImage == null)
                    formIconImage = PictureBoxIcon.Image;//赋初始值
                return formIconImage;
            }
            set
            {
                formIconImage = value;
                PictureBoxIcon.Image = formIconImage;
            }
        }

        /// <summary>
        /// 窗口名
        /// </summary>
        [Browsable(true)]
        [Description("窗口名")]
        public String FormName
        {
            get
            {
                if (formName == null) formName = "HAOestForm";
                return formName;
            }
            set
            {
                formName = value;
                LabelWindowName.Text = formName;
            }
        }
        #endregion

        #region 事件：最小化、最大化、关闭

        private void ButtonMin_MouseEnter(object sender, EventArgs e)
        {
            ButtonMin.Image = Properties.Resources.min_1;
        }

        private void ButtonMin_MouseLeave(object sender, EventArgs e)
        {
            ButtonMin.Image = Properties.Resources.min_0;
        }

        private void ButtonMin_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                this.FindForm().WindowState = FormWindowState.Minimized;
            }
        }

        private void ButtonMax_MouseEnter(object sender, EventArgs e)
        {
            if (this.FindForm().WindowState == FormWindowState.Normal)
            {
                ButtonMax.Image = Properties.Resources.max_1;
            }
            else if (this.FindForm().WindowState == FormWindowState.Maximized)
            {
                ButtonMax.Image = Properties.Resources.restore_1;
            }
        }

        private void ButtonMax_MouseLeave(object sender, EventArgs e)
        {
            if (this.FindForm().WindowState == FormWindowState.Normal)
            {
                ButtonMax.Image = Properties.Resources.max_0;
            }
            else if (this.FindForm().WindowState == FormWindowState.Maximized)
            {
                ButtonMax.Image = Properties.Resources.restore_0;
            }
        }

        private void ButtonMax_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                if (this.FindForm().WindowState == FormWindowState.Normal)
                {
                    this.FindForm().WindowState = FormWindowState.Maximized;
                    ButtonMax.Image = Properties.Resources.restore_0;
                    Invalidate();
                }
                else if (this.FindForm().WindowState == FormWindowState.Maximized)
                {
                    this.FindForm().WindowState = FormWindowState.Normal;
                    ButtonMax.Image = Properties.Resources.max_0;
                    Invalidate();
                }
            }
        }

        /// <summary>
        /// 双击标题栏
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void LabelWindowName_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (this.FindForm().WindowState == FormWindowState.Normal)
            {
                this.FindForm().WindowState = FormWindowState.Maximized;
                ButtonMax.Image = Properties.Resources.restore_0;
                Invalidate();
            }
            else if (this.FindForm().WindowState == FormWindowState.Maximized)
            {
                this.FindForm().WindowState = FormWindowState.Normal;
                ButtonMax.Image = Properties.Resources.max_0;
                Invalidate();
            }
        }

        private void ButtonClose_MouseEnter(object sender, EventArgs e)
        {
            ButtonClose.Image = Properties.Resources.close_1;
        }

        private void ButtonClose_MouseLeave(object sender, EventArgs e)
        {
            ButtonClose.Image = Properties.Resources.close_0;
        }

        private void ButtonClose_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                AnimateWindow(this.FindForm().Handle, 400, AW_BLEND | AW_HIDE | AW_CENTER);
                this.FindForm().Close();
            }
        }

        #endregion

        #region 事件：移动窗口
        private void LabelWindowName_MouseDown(object sender, MouseEventArgs e)
        {
            mouse = new MouseDrag(this.FindForm());
        }

        private void LabelWindowName_MouseUp(object sender, MouseEventArgs e)
        {
            mouse = null;
        }

        private void LabelWindowName_MouseMove(object sender, MouseEventArgs e)
        {
            SetCursor();
            if (e.Button == MouseButtons.Left)
            {
                mouse.Action(this.FindForm());
            }
        }

        #endregion

        #region 事件：调整窗口大小
        /// <summary>
        /// 记录按下鼠标的位置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FormContainer_MouseDown(object sender, MouseEventArgs e)
        {
            int left = this.FindForm().Left;
            int top = this.FindForm().Top;
            if (Mouse.Functions.GetMousePosition().X <= left + resizeBorderWidth)
                mouse = new MouseSizeLeft();
            else if (Mouse.Functions.GetMousePosition().X >= left + Width - 2 - resizeBorderWidth)
                mouse = new MouseSizeRight();
        }

        private void FormContainer_MouseMove(object sender, MouseEventArgs e)
        {

            if (CheckForReSize() && (e.Button == MouseButtons.Left))
            {
                mouse.Action(this.FindForm());
            }
        }
        private void TableLayoutPanelTop_MouseMove(object sender, MouseEventArgs e)
        {
            CheckForReSize();
        }

        private void ButtonMin_MouseMove(object sender, MouseEventArgs e)
        {
            CheckForReSize();
        }

        private void ButtonMax_MouseMove(object sender, MouseEventArgs e)
        {
            CheckForReSize();
        }

        private void ButtonClose_MouseMove(object sender, MouseEventArgs e)
        {
            CheckForReSize();
        }

        /// <summary>
        /// 改变鼠标形状，调整窗口大小
        /// </summary>
        private bool CheckForReSize()
        {
            SetCursor();
            if (mouse != null)
            {
                mouse.Action(this.FindForm());//执行事件响应
                return true;
            }
            else return false;
        }

        private void FormContainer_MouseUp(object sender, MouseEventArgs e)
        {
            mouse = null;
        }


        #endregion


        /// <summary>
        /// 窗体动画
        /// </summary>
        /// <param name="hwnd">窗口句柄</param>
        /// <param name="dwTime">不知道</param>
        /// <param name="dwFlags">不知道</param>
        /// <returns></returns>
        [System.Runtime.InteropServices.DllImport("user32")]
        public static extern bool AnimateWindow(IntPtr hwnd, int dwTime, int dwFlags);



        #region 公共方法
        /// <summary>
        /// 检查鼠标类型
        /// </summary>
        /// <param name="point">鼠标位置</param>
        /// <returns></returns>
        private Cursor CheckCursorType(Point point)
        {
            int top = this.FindForm().Top;
            int left = this.FindForm().Left;
            if ((point.X <= left + resizeBorderWidth + 4 && point.Y <= top + resizeBorderWidth + 4) || (point.X >= left + Width - resizeBorderWidth - 6 && point.Y >= top + Height - resizeBorderWidth - 6))
                return Cursors.SizeNWSE;
            else if ((point.X <= left + resizeBorderWidth + 4 && point.Y >= top + Height - resizeBorderWidth - 6) || (point.X >= left + Width - resizeBorderWidth - 6 && point.Y <= top + resizeBorderWidth + 4))
                return Cursors.SizeNESW;
            else if (point.X >= left + Width - 2 - resizeBorderWidth || point.X <= left + resizeBorderWidth)
                return Cursors.SizeWE;
            else if (point.Y >= top + Height - 2 - resizeBorderWidth || point.Y <= top + resizeBorderWidth)
                return Cursors.SizeNS;
            else
                return Cursors.Arrow;
        }

        /// <summary>
        /// 设置鼠标形状
        /// </summary>
        private void SetCursor()
        {
            this.FindForm().Cursor = CheckCursorType(Mouse.Functions.GetMousePosition());
        }
        #endregion

        /// <summary>
        /// 初始化父窗体
        /// by HADB
        /// 2012-9-23
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FormContainer_Load(object sender, EventArgs e)
        {
            this.FindForm().MinimumSize = this.MinimumSize;//设置父窗口的最小大小
            this.FindForm().FormBorderStyle = FormBorderStyle.None;//使父窗口不显示边框
            this.FindForm().StartPosition = FormStartPosition.CenterScreen;//使父窗口初始位置居中
            
        }


    }
}
